package com.test.exam;

import com.model.Person;

import java.util.*;
import java.util.stream.Collectors;

public class Work01_多层嵌套Map {


	public static int count=0;

	/**
	 * @param personCol
	 * 通过处理将传入的人类集合进行分组，先按照性别分组，再按照年龄分组，再按体重分组
	 * @return
	 */
	public static HashMap<String, HashMap<String, HashMap<String,Collection<Person>>>> sort(Collection<Person> personCol){
		//System.out.println( personCol);

		HashMap<String, HashMap<String, HashMap<String,Collection<Person>>>> result=new HashMap<String, HashMap<String, HashMap<String,Collection<Person>>>>();

		for(Person p : personCol) {
			HashMap<String, HashMap<String, Collection<Person>>> sexMap = result.get(p.getSex());
			if (sexMap == null) {
				sexMap = new HashMap<>();
				result.put(p.getSex(),sexMap);
			}

			HashMap<String, Collection<Person>> ageMap = sexMap.get(p.getAge());
			if (ageMap == null) {
				ageMap = new HashMap<>();
				sexMap.put(p.getAge(),ageMap);
			}

			Collection<Person> persons = ageMap.get(p.getWeight());
			if (persons == null) {
				persons = new ArrayList<>();
				ageMap.put(p.getWeight(),persons);
			}
			persons.add(p);
		}

		return result;
	}

	public static Collection<Person>  makeCol() {
		Collection<Person> personCol=new ArrayList<Person>();

		for(int i=0;i<100;i++) {
			personCol.add(makPerson());
		}
		return personCol;
	}


	public static Person makPerson() {
		Person p=new Person();

		if(count%2==0) {
			p.setSex("男");
		}else {
			p.setSex("女");
		}
		p.setAge(Math.round(Math.random()*5)+"");
		p.setWeight(Math.round(Math.random()*5)+"");
		return p;
	}


	/**
	 * @param args
	 */
	public static void main(String[] args) {

		HashMap<String, HashMap<String, HashMap<String, Collection<Person>>>> sort = sort(makeCol());
		for (String sex:sort.keySet()) {
			HashMap<String, HashMap<String, Collection<Person>>> sexMap = sort.get(sex);
			System.out.println("性别："+sex);
			for (String age: sexMap.keySet()) {
				HashMap<String, Collection<Person>> weightMap = sexMap.get(age);
				System.out.println("  年龄："+age);
				for (String weight : weightMap.keySet()) {
					System.out.println("    体重："+weight);
					Collection<Person> peoples = weightMap.get(weight);
					for (Person people:peoples) {
						System.out.print("       ");
						System.out.print(" 性别："+people.getSex()+" 年龄："+people.getAge()+" 体重："+people.getWeight()+" 姓名："+people.getName());
					}
					System.out.println();
				}
			}
		}
	}


}
